import os
import cv2
import mmcv 
from mmdet.apis import init_detector, inference_detector, show_result_pyplot

config_file = 'configs/myconfig/faster_rcnn_r50_fpn_coco.py'
checkpoint_file = 'checkpoints/faster_rcnn_x101_64x4d_fpn_1x_coco_20200204-833ee192.pth' 

model = init_detector(config_file, checkpoint_file, device='cpu')
# 测试视频和显示测试结果
#video = mmcv.VideoReader('/home/pv/pv_video/5.MP4') # mp4,avi,mov等常用的视频格式都可以！
#测试图片
filespath = "1.jpg"
dst_Path=os.path.join("result/", config_file.split("/")[-1].split(".")[0]) # 推断结果保存路径
img = mmcv.imread(filespath)
result = inference_detector(model, jpgpath)
# 推断结果保存，result是一个二维数组，多少个框，每个框的坐标和置信度。
#for label in range(len(result)):
#   bbox = result[label]
#   for i in range(bbox.shape[0]):
#       if bbox[i][4]>0.1:
#           cv2.rectangle(img,(bbox[i][0],bbox[i][1]),(bbox[i][2],bbox[i][3]),(0,0,255))

show_result_pyplot(model, filespath, result)  # 不保存测试的图片（帧）
